package com.arch.dev.client.view.chart;

import java.util.Set;

import com.arch.dev.client.controller.BudgetController;
import com.arch.dev.client.vo.MonthVO;
import com.arch.dev.client.vo.YearVO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class GraphView extends Composite {

	private static GraphViewUiBinder uiBinder = GWT
			.create(GraphViewUiBinder.class);

	interface GraphViewUiBinder extends UiBinder<Widget, GraphView> {
	}

	//@UiField
	//GChartExample01 graphWidget;
	
	@UiField(provided = true)
	ExpenditureChart graphWidget;
	
	public ExpenditureChart getGraphWidget() {
		return graphWidget;
	}

	public void setGraphWidget(ExpenditureChart graphWidget) {
		this.graphWidget = graphWidget;
	}

	@UiField(provided = true)
	Label yearlyLabel;
	@UiField
	ListBox yearlyListBox;

	@UiField(provided = true)
	Label monthlyLabel;
	@UiField
	ListBox monthlyListBox;

	ExpChartType expChartType;

	public GraphView(ExpChartType expChartType) {

		this.expChartType = expChartType;
		
		graphWidget = new ExpenditureChart(expChartType);
		
		final ExpChartType chartType = expChartType;
		yearlyLabel = new Label("Select the year");
		monthlyLabel = new Label("Select the month");
		// graphWidget = new GChartExample01();

		initWidget(uiBinder.createAndBindUi(this));
		graphWidget.update();
		yearlyListBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {

				if (chartType == ExpChartType.DAILY) {

					for (YearVO yearVO : yearVOs) {

						if (yearVO.getId() == Integer
								.parseInt(getSelectedYear())) {

							populateMonthlyListBox(yearVO);

						}

					}

				} else {

					BudgetController.getInstance()
							.getYearlyData(Integer.parseInt(getSelectedYear()), true,
									GraphView.this);

				}

			}
		});

		if (this.expChartType == ExpChartType.DAILY) {

			monthlyListBox.addChangeHandler(new ChangeHandler() {

				public void onChange(ChangeEvent event) {

					BudgetController.getInstance().getMonthlyDataForYear(
							Integer.parseInt(getSelectedYear()),
							Integer.parseInt(getSelectedMonth()),
							true,
							GraphView.this);

				}
			});

		} else {
			monthlyListBox.setVisible(false);
			monthlyLabel.setVisible(false);
		}

	}

	public GraphView(String monthly) {

		initWidget(uiBinder.createAndBindUi(this));

	}

	/*
	 * @UiHandler("listBox") void onClick(ClickEvent e) {
	 * Window.alert("Hello!"); }
	 */

	Set<YearVO> yearVOs;

	public void populateYearlyListBox(Set<YearVO> yearVOs) {

		this.yearVOs = yearVOs;

		yearlyListBox.clear();

		for (YearVO yearVO : yearVOs) {

			yearlyListBox.addItem(yearVO.getName(),
					Integer.toString(yearVO.getId()));

		}

		if (expChartType == ExpChartType.MONTHLY) {

			BudgetController.getInstance().getYearlyData(
					Integer.parseInt(getSelectedYear()), true, GraphView.this);

		} else {

			for (YearVO yearVO : yearVOs) {

				if (yearVO.getId() == Integer.parseInt(getSelectedYear())) {

					populateMonthlyListBox(yearVO);

				}

			}
		}

	}

	public void populateMonthlyListBox(YearVO yearVO) {

		monthlyListBox.clear();

		Set<MonthVO> monthVOs = yearVO.getMonths();

		for (MonthVO monthVO : monthVOs) {

			monthlyListBox.addItem(monthVO.getName(),
					Integer.toString(monthVO.getId()));

		}

		BudgetController.getInstance().getMonthlyDataForYear(
				Integer.parseInt(getSelectedYear()),
				Integer.parseInt(getSelectedMonth()), true, this);

	}

	private String getSelectedYear() {

		return yearlyListBox.getValue(yearlyListBox.getSelectedIndex());

	}

	private String getSelectedMonth() {

		return monthlyListBox.getValue(monthlyListBox.getSelectedIndex());

	}

	public void updateGraph() {
		graphWidget.update();
	}

}
